package start.spring.basic.demo.controller.demo;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import start.spring.basic.constant.ProConstant;
import start.spring.basic.demo.common.DemoConstant;
import start.spring.basic.demo.controller.BaseController;
import start.spring.basic.demo.entity.GgPzSys;
import start.spring.basic.demo.entity.Test;
import start.spring.basic.demo.service.demo.IDemoService;
import start.spring.basic.demo.vo.demo.DemoValidVO;
import start.spring.basic.log.ServiceLog;
import start.spring.basic.page.PageQuery;
import start.spring.basic.response.ResponseData;
import start.spring.basic.response.ResponsePage;

/**
 * 
 * ****************************************************************************
 * 开发样例请求入口
 *
 * @author(作者)：xuyongyun	
 * @date(创建日期)：2021年1月29日
 ******************************************************************************
 */
@Slf4j
@RestController
@RequestMapping("/web")
@Api(tags = "demo相关接口")
public class DemoController extends BaseController {

	@Autowired
	IDemoService demoService;
	
	@ServiceLog(businessType = "获取系统配置", systemType = DemoConstant.SYSTEM_TYPE, recordResult = true, userId = "")
	@RequestMapping(value = "/sys/config", method = {RequestMethod.GET})
	@ResponseBody
	@ApiOperation(value = "获取系统配置", notes = "获取GG_PZ_SYS表已加载到内存中指定pzdm的pzz")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pzdm", value = "配置的代码值", dataType = "String", required = true)
	})
	public ResponseData<String> getPzz(@RequestParam(required = true) String pzdm) {
		if(ProConstant.PRAMS.containsKey(pzdm)) {
			String pzz = ProConstant.PRAMS.getString(pzdm);
			return ResponseData.success("获取到配置值",pzz);
		}else {
			return ResponseData.errorMsg(pzdm + "配置代码不存在：");
		}
	}
	
	@ServiceLog(businessType = "参数校验", systemType = DemoConstant.SYSTEM_TYPE, recordResult = true, userId = "")
	@RequestMapping(value = "/prams/validate", method = {RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value = "参数校验", notes = "校验样例")
	public ResponseData<DemoValidVO> pramsValidate(@Valid DemoValidVO demoValid,BindingResult result) {
		if(result.hasErrors()) {
			return super.error(result);
		}
		return ResponseData.successData(demoValid);
	}
	
	@ServiceLog(businessType = "请求体纪录", systemType = DemoConstant.SYSTEM_TYPE, recordResult = false, userId = "")
	@RequestMapping(value = "/postbody/recode", method = {RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value = "测试网关记录请求体", notes = "样例")
	public ResponseData<JSONObject> postBodyRecode(@RequestBody String a) {
		log.info("请求参数：{}", a);
		return ResponseData.successData(JSON.parseObject(a));
	}
	
	@ServiceLog(businessType = "操作redis", systemType = DemoConstant.SYSTEM_TYPE, recordResult = true, userId = "")
	@RequestMapping(value = "/redis/set", method = {RequestMethod.GET})
	@ResponseBody
	@ApiOperation(value = "操作redis", notes = "同时操作多个redis")
	public ResponseData<JSONObject> setRedis() {
		return demoService.operateRedis();
	}
	
	@ServiceLog(businessType = "mp自定义id生成器", systemType = DemoConstant.SYSTEM_TYPE, recordResult = true, userId = "")
	@RequestMapping(value = "/mp/uid/generator", method = {RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value = "mp自定义id生成器", notes = "测试mybatis plus下使用UidGenerator，通过注解切换数据源")
	public ResponseData<String> mpUidGenerator() {
		return demoService.mpUidGenerator();
	}
	
	@ServiceLog(businessType = "jt自定义id生成器", systemType = DemoConstant.SYSTEM_TYPE, recordResult = true, userId = "")
	@RequestMapping(value = "/jt/uid/generator", method = {RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value = "jt自定义id生成器", notes = "测试jdbctemplete下使用UidGenerator,mybatis使用与此相同的方式，通过dbid切换数据源")
	public ResponseData<String> jtUidGenerator() {
		return demoService.jtUidGenerator();
	}
	
	@ServiceLog(businessType = "mp查询所有记录", systemType = DemoConstant.SYSTEM_TYPE, recordResult = false, userId = "")
	@RequestMapping(value = "/mp/query/all", method = {RequestMethod.GET})
	@ResponseBody
	@ApiOperation(value = "mp查询所有记录", notes = "mybatis plus查询某个表的所有记录")
	public ResponseData<List<GgPzSys>> mpQueryAll() {
		return demoService.MpQertyAll();
	}
	
	@ServiceLog(businessType = "mp分页查询", systemType = DemoConstant.SYSTEM_TYPE, recordResult = false, userId = "")
	@RequestMapping(value = "/mp/query/page", method = {RequestMethod.GET})
	@ResponseBody
	@ApiOperation(value = "mp分页查询", notes = "mybatis plus分页查询某个表的记录")
	public ResponsePage<GgPzSys> mpQueryPage(PageQuery pageQuery) {
		return super.returnResponse(demoService.mpQueryPage(pageQuery));
	}
	
	@ServiceLog(businessType = "mp切换数据源分页查询", systemType = DemoConstant.SYSTEM_TYPE, recordResult = false, userId = "")
	@RequestMapping(value = "/mp/multidb/query/page", method = {RequestMethod.GET})
	@ResponseBody
	@ApiOperation(value = "mp切换数据源分页查询", notes = "mybatis plus切换数据源分页查询某个表的记录")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "a", value = "测试字段a", dataType = "String", required = false)
	})
	public ResponsePage<Test> mpMultiDbQueryPage(PageQuery pageQuery,
			@RequestParam(required = false) String a) {
		return super.returnResponse(demoService.mpMultiDbQueryPage(pageQuery,a));
	}
	
	@ServiceLog(businessType = "mb切换数据源分页查询", systemType = DemoConstant.SYSTEM_TYPE, recordResult = false, userId = "")
	@RequestMapping(value = "/mb/multidb/query/page", method = {RequestMethod.GET})
	@ResponseBody
	@ApiOperation(value = "mb切换数据源分页查询", notes = "mybatis原生切换数据源分页查询")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "a", value = "测试字段a", dataType = "String", required = false)
	})
	public ResponsePage<Test> mbMultiDbQueryPage(PageQuery pageQuery,
			@RequestParam(required = false) String a) {
		return super.returnResponse(demoService.mbMultiDbQueryPage(pageQuery,a));
	}
	
	@ServiceLog(businessType = "jt分页查询", systemType = DemoConstant.SYSTEM_TYPE, recordResult = false, userId = "")
	@RequestMapping(value = "/jt/query/page", method = {RequestMethod.GET})
	@ResponseBody
	@ApiOperation(value = "jt分页查询", notes = "jdbctemplete分页查询")
	public ResponsePage<GgPzSys> jtQueryPage(PageQuery pageQuery) {
		return super.returnResponse(demoService.jtQueryPage(pageQuery));
	}
	
	@ServiceLog(businessType = "jt切换数据源分页查询", systemType = DemoConstant.SYSTEM_TYPE, recordResult = false, userId = "")
	@RequestMapping(value = "/jt/multidb/query/page", method = {RequestMethod.GET})
	@ResponseBody
	@ApiOperation(value = "jt切换数据源分页查询", notes = "jdbctemplete通过dbid切换数据源分页查询某个表的记录")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "a", value = "测试字段", dataType = "String", required = false)
	})
	public ResponsePage<Test> jtMultiDbQueryPage(PageQuery pageQuery,
			@RequestParam(required = false) String a) {
		return super.returnResponse(demoService.jtMultiDbQueryPage(pageQuery,a));
	}
	
	@ServiceLog(businessType = "jdbc跨数据库操作", systemType = DemoConstant.SYSTEM_TYPE, recordResult = true, userId = "")
	@RequestMapping(value = "/jdbc/operate", method = {RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value = "jdbc跨数据库操作", notes = "原生jdbc跨数据库操作")
	public ResponseData<String> jdbcOperate() {
		return demoService.jdbcOperate();
	}
}
